/*
 * DA3.c
 *
 * Created: 3/21/2015 10:16:02 AM
 *  Author: David Hunt
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>

void usart_init (void)
{
	UCSR0B = (1<<TXEN0);					//ENABLE TRANSMIT
	UCSR0C = (1<<UCSZ01) | (1<<UCSZ00) | (1<<UMSEL00);
	UBRR0L = 0x33;
}

ISR (TIMER1_COMPA_vect)
{
	//TRANSMIT UPON INTERRUPT
	DDRB = 0;			//PORT B INPUT TO READ DATA
	int data = PINB;
	while (!(UCSR0A & (1<<UDRE0)));
	UDR0 = data;		//SEND DATA
	DDRB = 0XFF;		//PORT B BACK TO OUTPUT
}

int main(void)
{
	//INITIALIZE ADC
	DDRD = 0;				//PORT D INPUT
	DDRB = 0xFF;			//PORT B OUTPUT
	ADCSRA = 0x87;			//ENABLE ADC AND PRESCALER
	ADMUX = 0xE0;			//2.56 V VREF AND ADC0 SINGLE ENDED
		
	//INITAILIZE USART	
	usart_init();
	
	//INITIALIZE TIMER1 INTERRUPT
	OCR1A = 0x3D08;						//SET TIMER TO RUN FOR 1 SECOND
	sei();								//ENABLE INTERRUPTS
	TCCR1B |= (1<<WGM12);				//CTC MODE
	TIMSK1 |= (1<< OCIE1A);				//INTERUPT ON COMPARE
	TCCR1B |= (1<< CS12) | (1<<CS10);	//PRESCALOR 1024 AND START TIMER

	
    while(1)
    {		
		ADCSRA |= (1<<ADSC);			//START CONVERSION
		while ((ADCSRA&(1<<ADIF))==0);	//WAIT FOR END OF CONVERSION
		PORTB = ADCH;					//GIVE HIGH BYTE TO PORTB
    }
}